<?php

/**
 * 拼多多快团团对接
 */
namespace App\Service\VendorClient\PddApi;

use App\Service\BaseService;
use Service\AppService\IdService;
use App\Service\TaskServer\TaskWorker;
use App\Service\AppService\SystemCacheConfigService;
class ShopPddKttUserApiClient extends BaseService
{
    public $config;
    public $bid;
    public $client_id;
    public $client_secret;
    public $access_token;
    public $bonded_warehouse_list;
    public $extends;
    public $shop;
    protected $method = [
        'updateStock'       => 'pdd.ktt.goods.incr.quantity',
        'uploadGoodsImg'    => 'pdd.ktt.goods.upload.image',
        'uploadGroupImg'    => 'pdd.ktt.group.upload.image',
    ];    
    public function __construct( $shop = null )
    {
        $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('pdd_ktt_config');
        $this->config = $config;
        $this->client_id                = $config['client_id'];
        $this->client_secret            = $config['client_secret'];
        $this->shop                     = $shop;
        $this->bid                      = $this->shop['bid'];
        $this->access_token             = $this->shop['token'];
    }
    
    /**
     * @签名
     */
    public function makeSign( $param = [] )
    {
        //公共参数
        $common=[
            'client_id'     =>  $this->client_id,
            'access_token'  =>  $this->access_token,
            'timestamp'     =>  time(),
        ];
        $param=array_merge($param,$common);
        ksort($param);
        $signData=$this->client_secret;
        foreach ($param as $k=>$v){
            $signData.=$k.$v;
        }
        $signData.=$this->client_secret;
        $sign=strtoupper(md5($signData));
        $param['sign']=$sign;
        return $param;
    }
    
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function execute( $param = [] )  
    {
        if( empty($param['data']) || empty($param['method']) ) {
            return Normal::returnCode(Normal::FAIL,"except param data or method");
        }
        
        $method         = $param['method'];
        if( !isset($this->method[$method]) ) {
            return Normal::returnCode(Normal::FAIL,"method {$method} not configured");
        }
        
        $data           = $param['data'];
        $data['type']   = $this->method[$method];
        if( !in_array($method,['getMallInfo','getAccessToken']) ) 
        {
            $data = $this->makeSign($data);
        }
        
        $url = $this->config['PDD_API_URL'];
        $parameter = [
            [
                'tag'       => 'pdd_'.$method,
                'uid'       => IdService::instance()->getOtherID(),
                'url'       => $url,
                'data'      => $data,
                'method'    => 'post',
                'format'    => 'x-www-form-urlencoded',
                'header'    => [],
            ],
        ];
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);
        $response   = @json_decode($resTask['response']['result'],true);
        if( isset($response['error_response']) ) {
            $error_msg = $response['error_response']['error_msg']??'';
            return Normal::returnCode(Normal::FAIL,$error_msg,$response);
        }
        return Normal::returnCode(Normal::SUC,"获取成功",$response);
    }
}